package com.avzipper;

import java.io.File;
import java.util.HashMap;

import android.os.Bundle;
import android.os.Environment;
import android.os.Handler;
import android.os.Message;
import android.app.Activity;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

public class MainActivity extends Activity {

	private Button btnEncode;
	private TextView tvFilePath;
	private ListView lvVideos;
	private ArrayAdapter<String> videoAdapter;
	private Compressor comp;
	private String selectedFileName;
	private File dir;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		
		btnEncode = (Button)findViewById(R.id.btnEncode);
		tvFilePath = (TextView)findViewById(R.id.txtFilePath);
		lvVideos = (ListView)findViewById(R.id.lvVideos);
		videoAdapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1);
		initListVideo();
		lvVideos.setAdapter(videoAdapter);
		lvVideos.setOnItemClickListener(new OnItemClickListener() {

			@Override
			public void onItemClick(AdapterView<?> adapter, View arg1, int pos, long arg3) {
				selectedFileName = videoAdapter.getItem(pos);
				tvFilePath.setText(selectedFileName);				
			}
		});
		
		btnEncode.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				startEncode();
			}
		});
				
		comp = new Compressor(this, handler);		
	}
	
	private void initListVideo(){
		dir = new File(Environment.getExternalStorageDirectory(), "external_sd/DCIM/100LGDSC");
		//dir = new File(Environment.getExternalStorageDirectory(), "/DCIM/Camera");
		if(dir.isDirectory()){
			File[] files = dir.listFiles();
			for(File file: files){
				if( file.getName().endsWith(".mp4") || file.getName().endsWith(".3gp"))
					videoAdapter.add(file.getName());				
			}
		}
		
	}
	
	private void startEncode(){
		
/*		HashMap<String, String> params = new HashMap<String, String>();
        File assFont = new File(Environment.getExternalStorageDirectory(), "DroidSansFallback.ttf");
		params.put("ass_default_font_path", assFont.getAbsolutePath());		
		params.put("aeskey", null);
*/		
        File inputVideoFile = new File(dir,selectedFileName);
		//String inputFileUrl = "file://" + inputVideoFile.getAbsolutePath();
		String inputFileUrl = inputVideoFile.getAbsolutePath();
		isSetingData = true;
		
		File outVideoFile = new File(dir, String.format("out%s",selectedFileName).replace("3gp", "mp4"));
		//String outFileUrl = "file://" + outVideoFile.getAbsolutePath();
		String outFileUrl = outVideoFile.getAbsolutePath();
		
		if(outVideoFile.exists())
			outVideoFile.delete();
		
		android.util.Log.i("MainActivity", "Convert from " + inputFileUrl + " to " + outFileUrl);		
		comp.startEncode(inputFileUrl, outFileUrl);
		//comp.setDataSource(inputFileUrl, outFileUrl, params);
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.main, menu);
		return true;
	}
	
	boolean isSetingData = false;
	boolean isConvertting = false;
	Handler handler = new Handler(){
		@Override
		public void handleMessage(Message msg) {
			switch(msg.what){
				case Compressor.MSG_SETDATA_DONE:
					this.removeMessages(Compressor.MSG_SETDATA_DONE);
					isSetingData = false;
					//File videoFile = new File(Environment.getExternalStorageDirectory(),"external_sd/DCIM/100LGDSC/result.mp4");
					//String fileUrl = "file://" + videoFile.getAbsolutePath();
					//android.util.Log.i("MainActivity", fileUrl);
					if(isSetingData || isConvertting)
						return;
					
					isConvertting = true;
					//comp.startEncode();
				break;
				
				case Compressor.MSG_CONVERT_DONE:
					this.removeMessages(Compressor.MSG_CONVERT_DONE);
					isConvertting = false;
					Toast.makeText(MainActivity.this, "Convert video done, please check!", Toast.LENGTH_LONG).show();
					break;
					
				case Compressor.MSG_OUTPUT_NOTSET:
					Toast.makeText(MainActivity.this, "Output file not set, please set it!", Toast.LENGTH_LONG).show();
					break;
			}
			//super.handleMessage(msg);
		}
	};

}
